package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.textfield.TextInputLayout;

import cn.lrwjz.androidguide.R;

public class TextFieldActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_field);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("TextField 示例");
        }

        // 设置基础文本框的错误提示
        TextInputLayout filledTextField = findViewById(R.id.filledTextField);
        filledTextField.getEditText().setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus) {
                String text = filledTextField.getEditText().getText().toString();
                if (text.isEmpty()) {
                    filledTextField.setError("用户名不能为空");
                } else {
                    filledTextField.setError(null);
                }
            }
        });

        // 设置带前缀和后缀的文本框
        TextInputLayout prefixTextField = findViewById(R.id.prefixTextField);
        prefixTextField.getEditText().setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus) {
                String text = prefixTextField.getEditText().getText().toString();
                if (!text.isEmpty()) {
                    try {
                        double amount = Double.parseDouble(text);
                        if (amount <= 0) {
                            prefixTextField.setError("金额必须大于0");
                        } else {
                            prefixTextField.setError(null);
                        }
                    } catch (NumberFormatException e) {
                        prefixTextField.setError("请输入有效的金额");
                    }
                }
            }
        });

        // 设置带图标的文本框
        TextInputLayout iconTextField = findViewById(R.id.iconTextField);
        iconTextField.setStartIconOnClickListener(v -> 
            Toast.makeText(this, "点击了搜索图标", Toast.LENGTH_SHORT).show()
        );

        // 设置密码文本框
        TextInputLayout passwordTextField = findViewById(R.id.passwordTextField);
        passwordTextField.getEditText().setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus) {
                String password = passwordTextField.getEditText().getText().toString();
                if (password.length() < 6) {
                    passwordTextField.setError("密码长度不能少于6位");
                } else {
                    passwordTextField.setError(null);
                }
            }
        });

        // 设置电子邮箱文本框
        TextInputLayout outlinedTextField = findViewById(R.id.outlinedTextField);
        outlinedTextField.getEditText().setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus) {
                String email = outlinedTextField.getEditText().getText().toString();
                if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
                    outlinedTextField.setError("请输入有效的电子邮箱地址");
                } else {
                    outlinedTextField.setError(null);
                }
            }
        });

        // 设置下拉菜单
        TextInputLayout dropdownLayout = findViewById(R.id.dropdownTextField);
        AutoCompleteTextView cityDropdown = (AutoCompleteTextView) dropdownLayout.getEditText();
        if (cityDropdown != null) {
            String[] cities = new String[]{"北京", "上海", "广州", "深圳", "杭州", "成都", "武汉", "西安"};
            ArrayAdapter<String> adapter = new ArrayAdapter<>(this, 
                android.R.layout.simple_dropdown_item_1line, cities);
            cityDropdown.setAdapter(adapter);
            cityDropdown.setOnItemClickListener((parent, view, position, id) -> {
                String selectedCity = cities[position];
                Toast.makeText(this, "已选择城市：" + selectedCity, Toast.LENGTH_SHORT).show();
            });
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 